import 'whatwg-fetch'
export const START_SEARCH = 'START_SEARCH'
export const RECEIVE_SEARCH = 'RECEIVE_SEARCH'
export const RECEIVE_BOOKINTRO = 'RECEIVE_BOOKINTRO'
export const RECEIVE_COMMENT = 'RECEIVE_COMMENT'
export const RECEIVE_BOOK_CHAPTER = 'RECEIVE_BOOK_CHAPTER'
export const RECEIVE_BOOK_CONTENT = 'RECEIVE_BOOK_CONTENT'


/**
 * 开始搜索
 * @param { 搜索关键词 } searchKeyowrd 
 */
export const startSearch = (searchKeyowrd,isShow) => {
    return {
        type:START_SEARCH,
        isShow:isShow,
        searchKeyowrd
    }
}

/**
 * 接收到 搜索结果
 * @param {搜索关键词} searchKeyowrd 
 * @param {返回的数据} list 
 */
const receiveSearch = (searchKeyowrd,list) => {
    return {
        type:RECEIVE_SEARCH,
        searchKeyowrd,
        list:list.books,
        receiveAt:Date.now()
    }
}

/**
 * 接收到的书籍介绍
 * @param { 书籍id } id 
 * @param { 介绍详情 } detail 
 */
export const receiveBookIntro = (id,isShow,detail) => {
    return {
        type:RECEIVE_BOOKINTRO,
        isShow:isShow,
        id:id,
        detail:detail,
        receiveAt:Date.now()
    }
}

/**
 * 接收评论内容
 * @param {评论的书籍id} id 
 * @param {评论内容} comment 
 */
export const receiveBookComment = (id,comment) => {
    return {
        type:RECEIVE_COMMENT,
        id:id,
        comments:comment.reviews,
        receiveAt:Date.now()
    }
}

/**
 * 
 * @param {书籍id} id 
 * @param {书籍章节信息} detail 
 */

const receiveBookContent = (chapter,isShow) => {
    return {
        type:RECEIVE_BOOK_CONTENT,
        chapter:chapter.chapter,
        isShow:isShow,
        receiveAt:Date.now()
    }
}

/**
 * 获取书籍章节列表
 * @param {书籍} id 
 * @param {章节list} list 
 */
const receiveBookChapterList = (id,isShow,list)=>{
    return {
        type:RECEIVE_BOOK_CHAPTER,
        id,
        isShow:isShow,
        list,
        receiveAt:Date.now()
    }
}

/**
 * 发起获取书籍介绍的请求
 * @param {书籍id} id 
 */
export const fetchBookIntro = (id) => {
    return dispatch => {
        return fetch(`/api/book/${id}`)
                .then(res => res.json())
                .then(json => dispatch(receiveBookIntro(id,false,json)))
    }
}

/**
 * 获取书籍id
 * @param {获取评论的书籍id} id 
 */
export const fetchBookComment = (id)=>{
    return dispatch => {
        return fetch(`/api/post/review/best-by-book?book=${id}&limit=10`)
                .then(res=> res.json())
                .then(json=>dispatch(receiveBookComment(id,json)))
    }
}

/**
 * 发起搜索数据的请求
 * @param {请求的关键词} searchKeyowrd 
 */
export const fetchBookList = (searchKeyowrd) =>{
    return dispatch => {
        dispatch(startSearch(searchKeyowrd,true))
        return fetch(`/api/book/fuzzy-search?query=${searchKeyowrd}`)
                .then(res => res.json())
                .then(json => {
                    dispatch(receiveSearch(searchKeyowrd,json))
                    dispatch(startSearch(searchKeyowrd,false))
                })
    }
}


/**
 * 获取主要内容
 * @param {第几章} url 
 */
export const fetchBookWord = (url)=>{
    return dispatch => {
        fetch(`/chapter/${url}`)
            .then(res=>res.json())
            .then(json=>dispatch(receiveBookContent(json,true)))
    }
}

/**
 * 获取章节信息
 * @param {书籍id} bookId 
 */
 const fetchBookContent = (id=[],chapter) => {
    let bookId = id[0]._id
    return dispatch => {
        fetch(`/api/btoc/${bookId}?view=chapters&channel=mweb`)
            .then(res=>res.json())
            .then(
                json=>{
                    dispatch(fetchBookWord(json.chapters[chapter]['link']))
                    dispatch(receiveBookChapterList(id,false,json.chapters))
                })
    }
 }

 /**
  * 获取书籍信息的具体id
  * @param {获取书籍具体章节信息的id} bookId 
  * @param {书籍的第几章} chapter
  */
export const fetchBookChapter = (bookId,chapter) => {
        return dispatch=>{
            fetch(`/api/btoc?view=summary&book=${bookId}`)
                .then(res=>res.json())
                .then(json=>dispatch(fetchBookContent(json,chapter)))
        }
}

